package studentInterface;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;

import com.registerserver.dto.SubjectAndMark;
import com.registerserver.service.IService;

public class ViewResultTotalMarks extends JFrame implements ActionListener{

	int width, height;
	JTable tblView;
	JPanel panelView;
	Object[][] cells;
	String[] columnNames = { "Môn học", "Mã học phần", "Số tín chỉ", "Điểm tổng kết", "Ghi chú" };
	JLabel lblAverageMark, lblTotalUnit, lblRanked;
	Box boxMain, boxView, boxNote;
	String studentID = null;
	static IService service;
	public ViewResultTotalMarks(String StudentID, IService service) throws Exception
	{	
		studentID = StudentID;
		this.service = service;
		initialInterface();
	}

	private void initialInterface() throws Exception
	{
		UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		width = getScreenWorkingWidth();
		height = getScreenWorkingHeight();
		setSize(width, height);
		setLocationRelativeTo(null);
		setTitle("Xem điểm tích lũy");
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		loadDataToTable();
		lookupRMI();
		Font font = new Font("Arial", Font.BOLD, 13);
		tblView = new JTable(cells, columnNames);
		lblAverageMark = new JLabel("Điểm tích lũy " + service.getOverAllMarkOfStudent(studentID));
		lblAverageMark.setFont(font);
		lblRanked = new JLabel("Xếp loại       ");
		lblRanked.setFont(font);
		lblTotalUnit = new JLabel("Tích lũy ");
		lblTotalUnit.setFont(font);
		boxMain = Box.createVerticalBox();
		boxNote = Box.createHorizontalBox();
		boxView = Box.createHorizontalBox();
		//tblView.setSize(new Dimension(width - 10, height -50));
		boxView.add(new JScrollPane(tblView));
		boxNote.add(lblAverageMark);
		boxNote.add(Box.createHorizontalStrut(30));
		boxNote.add(lblTotalUnit);
		boxNote.add(Box.createHorizontalStrut(30));
		boxNote.add(lblRanked);
		boxNote.add(Box.createHorizontalStrut(30));
		boxMain.add(boxView);
		boxMain.add(boxNote);
		add(boxMain);
		//add(lblAverageMark);
	}
	
	private void loadDataToTable() throws Exception
	{
		ResultSet rs = null;
		List<SubjectAndMark> data = service.getMarkOfAllSubject(studentID);
		if(data.size() == 0)
		{
			cells = new Object[1][5];
			cells[0][0] = "Không có dữ liệu";
			cells[0][1] = "Không có dữ liệu";
			cells[0][2] = "Không có dữ liệu";
			cells[0][3] = "Không có dữ liệu";
			cells[0][4] = "Không có dữ liệu";
		}
		else
		{
			int row = data.size();
			int col = 5;
			cells = new Object[row][col];
			int _row = 0;
			for(SubjectAndMark sa : data)
			{
				cells[_row][0] = sa.getSubjectName();
				cells[_row][1] = sa.getCourseID();
				cells[_row][2] = sa.getUnits();
				cells[_row][3] = sa.getMark();
				cells[_row][4] = "Ghi chú";
				_row++;
			}
		}
	}

	public static void main(String[] args) 
	{
		try {
			//String StudentID = "09095811";
			//new ViewResultTotalMarks(StudentID, service).setVisible(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		
	}
	
	public int getScreenWorkingWidth() 
	{
	    return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
	}

	public int getScreenWorkingHeight()
	{
	    return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height - 5;
	}
	private void lookupRMI() throws Exception
	{
		InitialContext ctx = new InitialContext();
		service = (IService) ctx.lookup("rmi://localhost:5555/RegisterCourse");
	}
}
